var mysql=require("mysql");  

var pool = null;

function nop(a,b,c,d,e,f,g){

}
  
function query(sql,callback){  
    pool.getConnection(function(err,conn){  
        if(err){  
            callback(err,null,null);  
        }else{  
            conn.query(sql,function(qerr,vals,fields){  
                //释放连接  
                conn.release();  
                //事件驱动回调  
                callback(qerr,vals,fields);  
            });  
        }  
    });  
};

exports.init = function(config){
    pool = mysql.createPool({  
        host: config.HOST,
        user: config.USER,
        password: config.PSWD,
        database: config.DB,
        port: config.PORT,
    });
};

exports.add_admin = function(account,password,privi,callback){
    callback = callback == null? nop:callback;
    if(account == null || password == null){
        callback(false);
        return;
    }
    
    var sql = ' SELECT * FROM t_admins WHERE account = "' + account + '" AND password = PASSWORD("' + password + '")';
    query(sql, function(err, rows, fields) {
        if (err) {
            if(err.code == 'ER_DUP_ENTRY'){
                callback(false);
                return;         
            }
            callback(false);
            throw err;
        }
        else{
            callback(true);  
        }
    });
};

exports.check_account = function(account,password,callback){
    callback = callback == null? nop:callback;
    if(account == null || password == null){
        callback(null);
        return;
    }
    
    var sql = ' SELECT * FROM t_admins WHERE account = "' + account + '" AND password = PASSWORD("' + password + '")';
    console.log(sql);
    query(sql, function(err, rows, fields) {
        if (err) {
            callback(false);
            throw err;
        }
        if(rows.length == 0){
            callback(null);
            return;
        }
        callback(rows[0]);
    });
};

exports.get_user_game_info = function(userid,callback){
    callback = callback == null? nop:callback;
    if(userid == null){
        callback(null);
        return;
    }

    var sql = 'SELECT userid,name,gems,headimg FROM t_users WHERE userid = ' + userid;
    query(sql, function(err, rows, fields) {
        if (err) {
            callback(null);
            throw err;
        }

        if(rows.length == 0){
            callback(null);
            return;
        }

        callback(rows[0]);
    });
};

exports.get_admin_by_token = function(token,callback){
    callback = callback == null? nop:callback;
    if(token == null){
        callback(null);
        return;
    }

    var sql = 'SELECT * FROM t_admins WHERE token = "' + token + '"';
    query(sql, function(err, rows, fields) {
        if (err) {
            callback(null);
            throw err;
        }

        if(rows.length == 0){
            callback(null);
            return;
        }

        callback(rows[0]);
    });
}

exports.update_token = function(account,token,callback){
    callback = callback == null? nop:callback;
    if(account == null || token == null){
        callback(false);
    }
    
    var sql = 'UPDATE t_admins SET token = "' + token +'" WHERE account = "' + account + '"';
    //console.log(sql);
    query(sql,function(err,rows,fields){
        if(err){
            callback(false);
            throw err;
        }
        else{
            callback(true);
        }
    });
};

exports.dec_gems = function(userid,gems,callback){
    callback = callback == null? nop:callback;
    var sql = 'UPDATE t_users SET gems = gems -' + gems + ' WHERE userid = ' + userid + " AND gems >= " + gems;
    query(sql, function(err, rows, fields) {
        if(err){
            callback(false);
            throw err;
        }
        else{
            callback(rows.affectedRows > 0);
        }
    });
};

exports.add_gems = function(userid,gems,callback){
    callback = callback == null? nop:callback;
    var sql = 'UPDATE t_users SET gems = gems +' + gems + ' WHERE userid = ' + userid;
    console.log(sql);
    query(sql, function(err, rows, fields) {
        if(err){
            callback(false);
            throw err;
        }
        else{
            callback(rows.affectedRows > 0);
        }
    });
};